package com.itcam.servlet_;




import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.Enumeration;

/**
 * Author:Cammy.Wu
 * Date:2024-06-27
 * Description:请求行请求头请求体
 */

public class ServletDemo03Request extends HttpServlet {

    @Override
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        System.out.println("post请求");
        this.doGet(request, response);
    }

    @Override
    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        System.out.println("get请求");
        // 获取请求中的请求方式
        String method = request.getMethod();
        System.out.println("method = " + method);
        // 获取请求中的完整地址
        StringBuffer requestURL = request.getRequestURL();
        System.out.println("requestURL = " + requestURL);
        // 获取请求中的资源路径
        String requestURI = request.getRequestURI();
        System.out.println("requestURI = " + requestURI);
        // 获取请求中的协议
        String scheme = request.getScheme();
        System.out.println("scheme = " + scheme);

        // ----------------------------------------------------------------------------
        System.out.println("----------------------------------------------------------------------------");

        // 获取请求头数据
        String userAgent = request.getHeader("user-agent");
        System.out.println("userAgent = " + userAgent);

        // 获取请求头信息中的所有key的枚举对象
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            String value = request.getHeader(key);
            System.out.println(key + ":" + value);
        }

        // ----------------------------------------------------------------------------
        System.out.println("----------------------------------------------------------------------------");

        // 获取用户请求数据
        // 无论请求方式是post还是get，获取用户的方式不变
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        System.out.println("name = " + name + ", pwd = " + pwd);

        // 获取用户数据中的所有key
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            System.out.println(parameterNames.nextElement());
        }

        // 获取相同key的多个数据值，例如checkbox
        String[] parameterValues = request.getParameterValues("fav");
        for (String parameterValue : parameterValues) {
            System.out.println("fav:" + parameterValue);
        }

        // ----------------------------------------------------------------------------
        System.out.println("----------------------------------------------------------------------------");


        // 其他常见方法
        // 获取远程客户端的地址
        String remoteAddr = request.getRemoteAddr();
        // 获取远程客户端的主机名称
        String remoteHost = request.getRemoteHost();
        // 获取远程客户端的端口号
        int remotePort = request.getRemotePort();
        System.out.println("remoteAddr = " + remoteAddr + ", remoteHost = " + remoteHost + ", remotePort = " + remotePort);

        String localAddr = request.getLocalAddr();
        String localName = request.getLocalName();
        System.out.println("localAddr = " + localAddr + ", localName = " + localName);
    }


}
